<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace App\Http\Service\frontend;

use App\Http\Models\Dances;
use App\Common\Utils\Common;
use Illuminate\Support\Facades\DB;
use App\Http\Models\Student\Student;
use App\Http\Models\Student\StudentBuy;
use App\Http\Models\Student\CourseBooking;

/**
 * Description of DancesService
 *
 * @author Admin
 */
class DancesService
{

    //获取课程列表
    public function index($request)
    {
        $page = $request->page ? $request->page : FRONTEND_PAGE;
        $limit = $request->limit ?? FRONTEND_LIMIT;
        $query = Dances::select('*')->with(['getUserBelongsTo' => function ($query) {
            $query->select(['id', 'name', 'username', 'images', 'level', 'synopsis']);
        }])
            ->where(['status' => 0])
            ->orderBy('sort', 'desc')
            ->orderBy('updated_at', 'desc');
        if ($request->type == 0 || $request->type == 1) {
            $query->where('types', $request->type);
        }
        if ($request->times) {
            $query->where('end_time', '>=', $request->times);
            $query->where('end_time', '<=', strtotime(date("Y-m-d 23:59:59", $request->times)));
        }
        $dancesDatas = Common::page($query, $page, $limit);
        foreach ($dancesDatas['list'] as $key => &$val) {
            $val['name'] = DANCE[$val['dance_names']] ?? "";
            $val['dance_names'] = DANCE[$val['dance_names']] ?? "";
            if ($val['get_user_belongs_to']) {
                $val['get_user_belongs_to']['images'] = $val['get_user_belongs_to']['images'] ? Common::getHost() . $val['get_user_belongs_to']['images'] : "";
            }
            $val['begin_time'] = date("Y-m-d H:i:s", $val['begin_time']);
            $val['end_time'] = date("Y-m-d H:i:s", $val['end_time']);
            $val['created_at'] = $val['created_at'];
            $val['updated_at'] = $val['updated_at'];
            $val['images'] = $val['images'] ? Common::getHost() . $val['images'] : "";
            $dancesDatas['list'][$key]['league_surplus_number'] = $val['league_number'] - $val['league_subscribe_number'];
        }
        return $dancesDatas;
    }

    //预约课程
    public function subscribeDances($request)
    {
        $dancesData = Dances::where(['id' => $request->dancesId, 'status' => 0])->first();
        $studentData = Student::where(['open_id' => $request->openId, 'status' => 0])->first();
        //判断是否授权
        if (!$studentData) {
            return [
                'code'    => 401,
                'message' => NOT_AUTHORIZATION
            ];
        }
        //判断完善信息
        if (!$studentData->phone) {
            return [
                'code'    => 402,
                'message' => NOT_INFORMATION
            ];
        }
        if (!$dancesData) {
            return [
                'code'    => 400,
                'message' => NOT_DANCES,
            ];
        }
        if ($dancesData->end_time < time()) {
            return [
                'code'    => 400,
                'message' => DANCES_TIME_OUT
            ];
        }
        //团课
        if ($dancesData->types == 0) {
            $where = [
                'student_id' => $studentData->id,
                'stu_status' => 0,
                'teacher_id' => $dancesData->dance_names,
                'status'     => 0
            ];
            //判断是否有购买次数
            $studentBuyData = StudentBuy::getStudentBuy($where);
            if (!$studentBuyData) {
                return [
                    'code'    => 400,
                    'message' => NOT_PURCHASE_NUMBER
                ];
            }
            $where = [
                'student_id' => $studentData->id,
                'stu_status' => 0,
                'status'     => 0,
                'teacher_id' => $dancesData->dance_names,
            ];
            //判断是否有购买次数
            $studentBuySumData = StudentBuy::getCountCurriculum($where);
            //检查团课人数是否已满
            if ($dancesData->league_number <= $dancesData->league_subscribe_number) {
                return [
                    'code'    => 400,
                    'message' => DANCES_NUMBER_SUCCESS,
                ];
            }
            //判断次数是否已完
            if ($studentBuySumData->surplus_number >= $studentBuySumData->member_number) {
                return [
                    'code'    => 400,
                    'message' => NUMBER_SUCCESS_OUT,
                ];
            }
            //判断是否已预约
            $couresBooking = CourseBooking::getStudentCourse($studentData->id, strtotime(date("Y-m-d 00:00:00", $dancesData->end_time)), strtotime(date("Y-m-d 23:59:59", $dancesData->end_time)), $dancesData->types, $dancesData->dance_names);
            if ($couresBooking) {
                return [
                    'code'    => 400,
                    'message' => COURSE_SUCCESS_OUT
                ];
            }
            //增加团课已约人数
            $dancesData->league_subscribe_number += 1;
            $dancesData->save();
        }
        //私课
        if ($dancesData->types == 1) {
            $where = [
                'student_id' => $studentData->id,
                'stu_status' => 1,
                'teacher_id' => $dancesData->teacher_id,
                'status'     => 0
            ];
            //判断是否有购买次数
            $studentBuyData = StudentBuy::getStudentBuy($where);
            //判断是否已预约
            $couresBooking = CourseBooking::getPrivateStudentCourse(
                $studentData->id,
                strtotime(date("Y-m-d 00:00:00", $dancesData->end_time)),
                strtotime(date("Y-m-d 23:59:59", $dancesData->end_time)),
                $dancesData->types,
                $dancesData->teacher_id
            );
            if ($dancesData->dance_status == 1) {
                return [
                    'code'    => 400,
                    'message' => '课程已被预约'
                ];
            }
            if ($couresBooking) {
                return [
                    'code'    => 400,
                    'message' => COURSE_SUCCESS_OUT
                ];
            }
            if (!$studentBuyData) {
                return [
                    'code'    => 400,
                    'message' => NOT_PURCHASE_NUMBER
                ];
            }
            //设置私教已预约
            $dancesData->dance_status = 1;
            $dancesData->save();
        }
        try {
            $data = [
                'student_id' => $studentData->id,
                'dances_id'  => $dancesData->dance_names,
                'teacher_id' => $dancesData->teacher_id,
                'begin_time' => $dancesData->begin_time,
                'end_time'   => $dancesData->end_time,
                'types'      => $dancesData->types,
                'dance'      => $dancesData->id,
                'created_at' => time()
            ];
            CourseBooking::create($data);
            $studentBuyData->surplus_number += 1;
            $studentBuyData->save();
            \App\Http\Models\User\User::where(['id' => $dancesData->teacher_id])->increment('by_number');
            DB::commit();
            return [
                'code'    => 200,
                'message' => SUBECRIBE_SUCCESS
            ];
        } catch (\Exception $ex) {
            DB::rollBack();
            return [
                'code'    => 400,
                'message' => $ex->getMessage()
            ];
        }
    }

    public function show($id)
    {
        $dancesData = Dances::select('*')->with(['getUserBelongsTo' => function ($query) {
            $query->select(['id', 'name', 'username', 'images', 'level', 'synopsis']);
        }])->where(['id' => $id, 'status' => 0])->first();
        if ($dancesData) {
            $dancesData = $dancesData->toArray();
            if ($dancesData['get_user_belongs_to']) {
                $dancesData['get_user_belongs_to']['images'] = $dancesData['get_user_belongs_to']['images'] ? Common::getHost() . $dancesData['get_user_belongs_to']['images'] : "";
            }
            $dancesData['begin_time'] = date("Y-m-d H:i:s", $dancesData['begin_time']);
            $dancesData['end_time'] = date("Y-m-d H:i:s", $dancesData['end_time']);
            $dancesData['created_at'] = $dancesData['created_at'];
            $dancesData['updated_at'] = $dancesData['updated_at'];
            $dancesData['dance_names'] = DANCE[$dancesData['dance_names']] ?? "";;
            $dancesData['images'] = $dancesData['images'] ? Common::getHost() . $dancesData['images'] : "";
            $dancesData['league_surplus_number'] = $dancesData['league_number'] - $dancesData['league_subscribe_number'];
            return $dancesData;
        }
        return false;
    }

    //购买记录
    public function purchaseLog()
    {

    }

}
